package com.company;

public class Main {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        // write your code here
        ListNode nodeSta = new ListNode(1);
        ListNode nextNode;
        nextNode=nodeSta;

        //创建链表
        for(int i=1; i <= 6; i++){
            if(i <= 3){
                ListNode node = new ListNode(i+1);
                nextNode.next = node;
                nextNode=nextNode.next;
            }else{
                ListNode node = new ListNode(6-i+1);
                nextNode.next = node;
                nextNode=nextNode.next;
            }

        }
        nextNode=nodeSta;
        print(nextNode);
        if(isPalindrome(nextNode)){
            System.out.println("是回文链表");
        }else{
            System.out.println("不是回文链表");
        }
    }

    public static boolean isPalindrome(ListNode head) {
        int i = 0;
        int len = 0;
        ListNode p;
        int[] arr = new int[100001];
        p = head;
        while (p != null) {
            arr[i] = p.val;
            i++;
            p = p.next;
        }
        len = i;
        for (i = 0; i < len; i++) {
            if (arr[i] != arr[len - i - 1]) return false;
        }
        return true;
    }

    public static void print(ListNode n){
        while(n != null){
            System.out.println("节点:" + n.val);
            n = n.next;
        }
        System.out.println();
    }
}